package com.example.hrandroidapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.hrandroidapp.adapter.ImageAdapter;
import com.example.hrandroidapp.common.CheckAuthentication;
import com.example.hrandroidapp.common.HRLog;
import com.example.hrandroidapp.domain.LeavePO;
import com.example.hrandroidapp.preferences.UserPreference;
import com.gc.materialdesign.views.FloatingActionButton;

public class MainActivity extends ActionBarActivity {
	
	private NotificationManager mNotificationManager;
	private int notificationID = 100;
	private GridView grdView;
	private Resources res;
	
	private FloatingActionButton btnFloatLeave, btnFloatOT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		HRLog.postLogInfo("check Auth: " + CheckAuthentication.isAuthentication(this), "MainActivity.onCreate");
		if(!CheckAuthentication.isAuthentication(this)) {
			finish();
			overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
		}
		
		loadViews();
		
		btnFloatLeave.setIcon(R.drawable.leave_dark);
		btnFloatLeave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), CreateLeaveActivity.class);
				startActivity(intent); 
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}
		});
		
		btnFloatOT.setIcon(R.drawable.overtime_dark);
		btnFloatOT.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), CreateOverTimeActivity.class);
				startActivity(intent); 
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}
		});
		
		grdView.setAdapter(new ImageAdapter(this, res.getStringArray(R.array.main_menu_arr)));
		grdView.setOnItemClickListener(new OnItemClickListener());
	}
	
	private void loadViews() {
		btnFloatLeave = (FloatingActionButton) findViewById(R.id.btn_float_leave);
		btnFloatOT = (FloatingActionButton) findViewById(R.id.btn_float_ot);
		grdView = (GridView) findViewById(R.id.gridview);
		res = getResources();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		if(id == R.id.action_logout) {
			UserPreference.clearUser(getApplicationContext());
			finish();
			overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
		}
		if(id == R.id.action_noti) {
			displayNotification();
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {}
	
	private class OnItemClickListener implements AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if(position == 0) {		// leave approval
				Intent intent = new Intent(getApplicationContext(), LeaveApprovalActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}else if(position == 1) {	// Attendance
				Intent intent = new Intent(getApplicationContext(), AttendanceActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}else if(position == 2) {	// Calendar
				Intent intent = new Intent(getApplicationContext(), MyCalendar.class);
				startActivity(intent);
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}else if(position == 3) {		// Overtime
				Intent intent = new Intent(getApplicationContext(), OvertimeActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
			}
		}
		
	}
	
	protected void displayNotification() {
        Log.i("Start", "notification");

      /* Invoking the default notification service */

        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setContentTitle("New Message");
        mBuilder.setContentText("You've received new message.");
        mBuilder.setTicker("New Message Alert!");
        mBuilder.setSmallIcon(R.drawable.ic_launcher);

      /* Increase notification number every time a new notification arrives */

      /* Creates an explicit intent for an Activity in your app */
        Intent resultIntent = new Intent(this, LeaveMailDetailActivity.class);
        
        LeavePO leavePO = new LeavePO();
        leavePO.setEmployeeName("Name");
    	leavePO.setReason("Subject ");
    	leavePO.setLeaveDescription("Pending Message ");
    	leavePO.setStartDateTime("20150101");
    	
    	resultIntent.putExtra("leave", leavePO);
    	resultIntent.putExtra("pending", "1");

        android.app.TaskStackBuilder stackBuilder = android.app.TaskStackBuilder.create(this);
        stackBuilder.addParentStack(LeaveApprovalActivity.class);

      /* Adds the Intent that starts the Activity to the top of the stack */
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

      /* notificationID allows you to update the notification later on. */
        Notification noti = mBuilder.getNotification();
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(notificationID, noti);

        mBuilder.setAutoCancel(true);
    }
}
